/**
 * Copyright 2016-2017 TVI Go Easy.
 * Created on 2017/4/18 20:07
 */
package org.mechanic.fund.service.product.impl;

import org.mechanic.core.Constants;
import org.mechanic.exception.NoSqlResultException;
import org.mechanic.fund.domain.product.ProductStocksIndex;
import org.mechanic.fund.domain.product.ProductStocksIndexType;
import org.mechanic.fund.domain.utils.mapper.DtoBeanMapper;
import org.mechanic.fund.repository.product.ProductStocksIndexRepository;
import org.mechanic.fund.service.product.IProductStocksIndexService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * 基准信息服务实现
 *
 * @author mechanic
 * @version 0.0.1
 */
@Service
public class ProductStocksIndexService implements IProductStocksIndexService {

    private static Logger logger = LoggerFactory.getLogger(ProductStocksIndex.class);

    private ProductStocksIndexRepository productStocksIndexRepository;

    @Autowired
    public ProductStocksIndexService(ProductStocksIndexRepository productStocksIndexRepository) {
        this.productStocksIndexRepository = productStocksIndexRepository;
    }

    /**
     * 返回每类基准信息最新的一条记录
     *
     * @return 每类基准信息最新的一条记录
     */
    @Override
    @Cacheable(value = Constants.EHCACHE_NAME)
    public Page<ProductStocksIndex> getNewestListByType(Pageable pageable) {
        return this.productStocksIndexRepository.findNewestByProductStocksIndexType(pageable);
    }

    /**
     * 返回匹配指定基准信息的基准信息集合
     *
     * @param typeId 基准信息类型编号
     * @return 指定基准信息的基准信息集合
     */
    @Override
    @Cacheable(value = Constants.EHCACHE_NAME)
    public Page<ProductStocksIndex> getListByTypeId(String typeId, Pageable pageable) {
        Assert.hasLength(typeId, "typeId 不能为空");

        ProductStocksIndexType type = new ProductStocksIndexType();
        type.setId(typeId);

        Page<ProductStocksIndex> productStocksIndices = this.productStocksIndexRepository.findByProductStocksIndexTypeId(
          type, pageable);
        if (productStocksIndices == null) {
            throw new NoSqlResultException("没有数据。");
        }
        return productStocksIndices;
    }

    /**
     * 批量保存基准信息
     *
     * @param productStocksIndices 基准信息集合
     */
    @Override
    public void save(List<ProductStocksIndex> productStocksIndices, String operationUserId) {
        this.productStocksIndexRepository.save(DtoBeanMapper.setOperationToList(productStocksIndices, operationUserId));
    }

    /**
     * 删除基准信息
     *
     * @param id 主键编号
     */
    @Override
    public void delete(String id) {
        try {
            this.productStocksIndexRepository.delete(id);
        } catch (EmptyResultDataAccessException e) {
            logger.info(id + "不存在");
        }

    }

    /**
     * 按ID查找基准信息
     *
     * @return 每类基准信息最新的一条记录
     */
    @Override
    @Cacheable(value = Constants.EHCACHE_NAME)
    public ProductStocksIndex getById(String id) {
        return this.productStocksIndexRepository.findOne(id);
    }
}
